package com.jy.modules.cims.component.settle;

import java.io.OutputStream;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.jy.modules.cims.common.sysUtils.FileUtils;
import com.jy.modules.cims.data.dto.AnnotationDto;
import com.jy.modules.cims.data.dto.Df02FileDto;
import com.jy.modules.cims.data.dto.DfHzContentDto;
import com.jy.modules.cims.data.dto.Ds02FileDto;
import com.jy.modules.cims.data.dto.DsHzContentDto;
/**
 * @description 广银联 报盘回盘文件解析及生成
 * @author zhuwenbin
 *
 */
@Service("com.jy.modules.cims.component.settle.CGYLFileOperImpl")
public class CGYLFileOperImpl {
	

	 private static final Logger logger =  LoggerFactory.getLogger(CGYLFileOperImpl.class);
    /**
     * 将数组转化为DTO对象
     * @param clazz
     * @param str
     * @return
     * @throws InstantiationException
     * @throws IllegalAccessException
     */
	public Object stringConvertDTO(Class clazz,String[] str ) throws  InstantiationException, IllegalAccessException{
		
		Object obj = clazz.newInstance();
		Field[] fields  = obj.getClass().getDeclaredFields();
		for(Field field :  fields){
			boolean flag = field.isAccessible();
			field.setAccessible(true);
			//获取字段所对应数组中的索引
			AnnotationDto annotation =  field.getAnnotation(AnnotationDto.class);
			//从数组中取值置入对象
			field.set(obj, str[annotation.index()]);
			field.setAccessible(flag);  
		}
		return obj;
		
	}
	
	/**
	 * @description DTO对象转换为StringBuffer字段用regex分割
	 * @param obj
	 * @param regex
	 * @return
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 */
	public StringBuffer DtoConvertString(Object obj,String regex) throws IllegalArgumentException, IllegalAccessException{
		StringBuffer stringBuffer = null;
		if(regex == null || "".equals(regex) || obj == null){
			logger.error("分隔符为空！ ");
			return stringBuffer;
		}
		
		stringBuffer = new StringBuffer();
		Field[] fields = obj.getClass().getDeclaredFields();
		for(Field field : fields){
			
			String value = (String) field.get(obj);
			if(value ==null){
				stringBuffer.append("");
			}else{
				stringBuffer.append(value);
				
			}
			
			stringBuffer.append(regex);
		}
		//删除最后一位分割符
		stringBuffer.replace(stringBuffer.lastIndexOf(regex), stringBuffer.length(), "");
		return stringBuffer;
		
	}
	
	/**
	 * @description 解析代收文件
	 * @param fileFullName
	 * @param regex
	 * @param limit
	 * @return
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 */
    public List<Object> analyDsFile(String fileFullName,String regex,int limit) throws InstantiationException, IllegalAccessException{
    	
    	//按分隔符解析文件生成数组列表
    	List<String[]> dataList =  FileUtils.analyRegexFile(fileFullName,regex,limit);
    	List<Object> objList = new ArrayList<Object>();
    	int i = 0;
    	Ds02FileDto dsdto = null;
    	DsHzContentDto dshz = null;
    	for(i = 0;i<dataList.size(); i++){
    		if(i==0){
    			//第一行解析文件汇总
    			 dshz = (DsHzContentDto) stringConvertDTO(DsHzContentDto.class, dataList.get(i));
    			 objList.add(dshz);
    		}else{
    			 //解析文件内容
    			 dsdto = (Ds02FileDto) stringConvertDTO(Ds02FileDto.class, dataList.get(i));
    			 objList.add(dsdto);
    		}
    		
    	}
       return objList;
    	
    }
    
    
    
    /**
	 * @description 解析代付文件
	 * @param fileFullName
	 *        文件全路径名称
	 * @param regex
	 *        分隔符
	 * @param limit
	 *        列数
	 * @return
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 */
    public List<Object> analyDfFile(String fileFullName,String regex,int limit) throws InstantiationException, IllegalAccessException{
    	
    	//按分隔符解析文件生成数组列表
    	List<String[]> dataList =  FileUtils.analyRegexFile(fileFullName,regex,limit);
    	List<Object> objList = new ArrayList<Object>();
    	int i = 0;
    	Df02FileDto dfdto = null;
    	DfHzContentDto dfhz = null;
    	for(i = 0;i<dataList.size(); i++){
    		if(i==0){
    			//第一行解析文件汇总
    			 dfhz = (DfHzContentDto) stringConvertDTO(DfHzContentDto.class, dataList.get(i));
    			 objList.add(dfhz);
    		}else{
    			 //解析文件内容
    			 dfdto = (Df02FileDto) stringConvertDTO(Df02FileDto.class, dataList.get(i));
    			 objList.add(dfdto);
    		}
    		
    	}
       return objList;
    	
    }
    
    
	
    /**
     * @description 生成报盘文件
     * @param objList
     * @param fileFullName
     *        文件全路径
     * @param regex
     *        分隔符号
     * @return
     * @throws IllegalArgumentException
     * @throws IllegalAccessException
     */
    public boolean createBPFile(List<Object> objList,OutputStream os,String regex) throws IllegalArgumentException, IllegalAccessException{
    	boolean flag = false;
    	List<StringBuffer> strList =  new ArrayList<StringBuffer>(objList.size());
    	for(Object obj : objList){
    		//将dto装化为字符串列表
    		StringBuffer strBuffer = DtoConvertString(obj, regex);
    		strList.add(strBuffer);
    	}
    	//将字符串列表写入文件
    	flag = FileUtils.writeFile(strList,os);
    	
    	return flag;
    }

}
